package com.anssy.voteballot.base;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.anssy.voteballot.R;
import com.anssy.voteballot.utils.StatusBarUtils;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.RequestBody;


public abstract class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutResource());
//        configNoActionBar();
        initView();
        initData();
        initListener();
    }


    private void configNoActionBar() {
        Window window = getWindow();
        View decorView = window.getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }


    public abstract int setLayoutResource();

    public abstract void initView();

    public abstract void initData();

    public abstract void initListener();


    /**
     * 配置toolBar
     *
     * @param title
     * @param activity
     */
    public void configBaseToolBar(String title, final Activity activity) {
        Toolbar toolBar = findViewById(R.id.toolbar);
        TextView mTvTitle = findViewById(R.id.toolbar_title);
        mTvTitle.setText(title);
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    /**
     * 配置toolBar
     *
     * @param title
     * @param activity
     */
    public void configBaseToolBarNoIcon(String title, Activity activity) {
        TextView mTvTitle = findViewById(R.id.toolbar_title);
        mTvTitle.setText(title);

    }

    public RequestBody setRequestBody(String reqJson) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), reqJson);
        return body;
    }

    public RequestBody setRequestJson(Object object) {
        String reqJson = new Gson().toJson(object);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), reqJson);
        return body;
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatusBarUtils.setStatusTextColor(true, BaseActivity.this);
    }
}
